package Servlet;

import Dao.BillDao;
import Model.Bill;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Map;


@WebServlet("/AdminCountRecordServlet")
public class AdminCountRecordServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 获取会话中的用户ID
        HttpSession session = request.getSession(false);
        Integer UserID = (Integer) session.getAttribute("UserID");

        // 创建 Bill 对象
        Bill bill = new Bill();
        bill.setUserId(UserID);

        // 从请求体中解析 JSON 数据
        StringBuilder requestBody = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            requestBody.append(line);
        }

        // 将 JSON 数据解析为 Bill 对象
        JSONObject requestJson = new JSONObject(requestBody.toString());
        if (requestJson.has("startDate") && !requestJson.isNull("startDate")) {
            bill.setStartDate(requestJson.getString("startDate"));
        }
        if (requestJson.has("endDate") && !requestJson.isNull("endDate")) {
            bill.setEndDate(requestJson.getString("endDate"));
        }
        if (requestJson.has("itemName") && !requestJson.isNull("itemName")) {
            bill.setItemName(requestJson.getString("itemName"));
        }
        if (requestJson.has("seller") && !requestJson.isNull("seller")) {
            bill.setSeller(requestJson.getString("seller"));
        }
        if (requestJson.has("purchaser") && !requestJson.isNull("purchaser")) {
            bill.setPurchaser(requestJson.getString("purchaser"));
        }
        if (requestJson.has("money") && !requestJson.isNull("money")) {
            bill.setMoney(new BigDecimal(requestJson.getString("money")));
        }

        // 从数据库中获取记录列表
        Map<String, Object> results = BillDao.selectNum(bill);

        // 使用 Jackson 库将记录列表转换为 JSON 格式
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(results);

        // 设置响应内容类型为 JSON
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        // 将 JSON 数据写入响应
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
    }
}